import axios from '../../api/request';
import { getToken } from '../../utils/storageUtils'

let formDom = document.querySelector('#formDom');
let usernameInp = document.querySelector('#username');
let pwdInp = document.querySelector('#pwd');
let repwdInp = document.querySelector('#repwd');
let profileInp = document.querySelector('#profile');
let profileImg = document.querySelector('#profileImg');//profileImg


profileInp.onchange =function(e){

    // 创建读取文件对象
    let readFile = new FileReader();

    // 通过事件对象获取文件
    let file = e.target.files[0];

    // 确定采用DataUrl方式读取文件
     readFile.readAsDataURL(file);


    readFile.onload = function(){
        
        // console.log( readFile.result);
        profileImg.src =  readFile.result;
    }


}


formDom.onsubmit =async function(e){
    e.preventDefault();

    let username =usernameInp.value;
    let pwd =pwdInp.value;
    let repwd =repwdInp.value;
    let profile =profileInp.value;

    if(!username || !pwd || !repwd || !profile){
        // console.log("请保持表单完整");
        alert("请保持表单完整")
        // repwdInp.focus()
        return;
    }

    if(pwd !== repwd){
        alert('两次输入密码不一致')
        return;
    }
    

    let formData = new FormData(formDom);

    let {data,code} = await axios({
        method:"POST",
        url:"/admin/user",
        data:formData,
    })

    // console.log(code);
    if(code===2000){
        window.location.href = '/index.html';
    }

}


